<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <!--    <link rel="shortcut icon" href="favicon.ico">-->
    <script type="text/javascript" src="/js/jquery.min.js"></script>
    <script type="text/javascript" src="/js/bootstrap.min.js"></script>
    <link href="/css/bootstrap.min.css" rel="stylesheet">
    <link href="/css/font-awesome.css" rel="stylesheet">
    <link href="/css/plugins/sweetalert/sweetalert.css" rel="stylesheet">
    <link href="/css/animate.css" rel="stylesheet">
    <link href="/css/style.css" rel="stylesheet">
</head>
<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInUp">
    <div class="row">

        <div class="col-lg-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>新增/编辑</h5>

                    <div class="ibox-tools">
                        <a th:href="@{/constantitem/index}" class="btn btn-primary btn-xs">返回</a>
                    </div>
                </div>
                <div class="ibox-content">
                    <div class="row ">
                        <div class="col-md-10">
                            <form class="form-horizontal m-t-md" th:action="@{/constantitem/add}" method="post"
                                  id="commentForm">
                                <input type="hidden" name="id" id="id" th:value="${constantitem.id!=null?constantitem.id:''}"/>
                                <div class="form-group">
                                    <label class="col-sm-2 col-sm-2 control-label">
                                        <em style="color: red;">*</em>
                                        常数项编码
                                    </label>
                                    <div class="col-sm-5">
                                        <input name="ConstantCode" id="ConstantCode" type="text" class="form-control m-b"
                                               placeholder="请输入药品编码" required="请输入药品编码" th:value="${constantitem.constantCode!=null?constantitem.constantCode:''}">
                                    </div>
                                </div>

                                <div class="form-group">
                                    <label class="col-sm-2 col-sm-2 control-label">
                                        <em style="color: red;">*</em>
                                        常数项名称
                                    </label>
                                    <div class="col-sm-5">
                                        <input name="ConstantName" id="ConstantName" type="text" class="form-control  m-b"
                                               placeholder="请输入药品名称" required="请输入药品名称" th:value="${constantitem.constantName!=null?constantitem.constantName:''}">
                                    </div>
                                </div>



                                <div class="form-group">
                                    <label class="col-sm-2 col-sm-2 control-label">
                                        <em style="color: red;">*</em>
                                        包装单位
                                    </label>
                                    <div class="col-sm-5">
                                        <select name="ConstantTypeID" id="ConstantTypeID" class="form-control">
                                            <option th:value="${constanttype.id}" th:each="constanttype : ${constanttypes}" th:text="${constanttype.constantTypeName}"></option>
                                        </select>
                                    </div>
                                </div>

                                <div class="form-group">
                                    <label class="col-sm-2 col-sm-2 control-label"></label>
                                    <div class="col-sm-10">
                                        <button type="button" id="btnCheck" onclick="add()" class="btn btn-group btn-primary">
                                            提交
                                        </button>
                                        <button onclick="restForm()" class="btn btn-primary btn-primary">重置</button>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<!-- jQuery Validation plugin javascript-->
<script src="/js/plugins/validate/jquery.validate.min.js"></script>
<script src="/js/plugins/validate/messages_zh.min.js"></script>
<!-- Sweet alert -->
<script src="/js/plugins/sweetalert/sweetalert.min.js"></script>
<script type="text/javascript" th:inline="javascript">
    $(function () {
        var constantTypeID = [[${constantitem.ConstantTypeID}]];

        $("#fixed option[value= " + constantTypeID + "]").prop("selected",true);
    });

    //重置
    function restForm(){
        $("#commentForm")[0].reset();
    }

    //新增
    function add() {

        const url = [[@{/constantitem/add}]];
        var data = getData();

        $.ajax({
            type: "post",
            url: url,
            async: true,
            dataType: "JSON",
            data: JSON.stringify(data),
            contentType: "application/json",
            error: function (data) {
                swal({
                        title: "网络错误，请稍后重试！",
                        type: "error"
                    })
            },
            success: function (data) {
                if (data.succ) {
                    const url2 = [[@{/constantitem/index}]];
                    window.location.href = url2;

                    swal({
                        title: data.msg,
                        type: "success"
                    });

                }else{
                    swal({
                        title: data.msg,
                        type: "warning"
                    })
                }

            }//success:function

        });
    }

    function getData() {
        var id = $("#id").val();
        var ConstantCode = $("#ConstantCode").val();
        var ConstantName = $("#ConstantName").val();
        var ConstantTypeID = $("#ConstantTypeID").val();

        var data = {
            id: id,
            constantTypeID: ConstantTypeID,
            constantCode: ConstantCode,
            constantName: ConstantName
        }

        return data;
    }
</script>
</body>
</html>
